package com.yunkeji.api.common.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yunkeji.api.common.bo.DicQueryBo;
import com.yunkeji.api.common.vo.DictData;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.dao.domain.sys.SysDictData;
import com.yunkeji.dao.mapper.sys.SysDictDataMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典查询
 *
 * @author 老张
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/dict")
public class DictDataController extends BaseController {

    private final SysDictDataMapper selectDictDataByType;

    /** 根据字典类型查询字典数据信息 */
    @PostMapping(value = "/type")
    public R<Map<String, List<DictData>>> dictType(@RequestBody @Valid DicQueryBo bo) {
        List<List<DictData>> dataList = new ArrayList<>();
        List<String> dictTypes = bo.getDictTypes();
        Map<String, List<DictData>> datas = new HashMap<>();
        dictTypes.forEach(
                item -> {
                    List<SysDictData> data = selectDictDataByType.selectDictDataByType(item);
                    if (ObjectUtil.isNull(data)) {
                        data = new ArrayList<>();
                    }
                    List<DictData> dictData = BeanUtil.copyToList(data, DictData.class);
                    dataList.add(dictData);
                    datas.put(item, dictData);
                });

        return R.ok(datas);
    }
}
